﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.VirtualTeam;
using BLL.VirtualTeam.Impl;
using Model;
using Utils;
using System.Text;

public partial class Page_RFP_RFPDetail : System.Web.UI.Page
{

    static string RFPId = "";
    List<RFP> rfpList = new List<RFP>();
    protected void Page_Load(object sender, EventArgs e)
    {

            if (Request["RFPId"] != null)
            {
                RFPId = Request["RFPId"].ToString();
            }
            RFPDataBind();
            RFPReplyDataBind();
            UIControl();
            RFPTeamBind();
    }

    protected void LBReplier_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string replyExpertId = bt.CommandArgument.ToString();
        Response.Redirect("../Expert/ExpertDetails.aspx?expertId=" + replyExpertId);
    }

    private void UIControl()
    {
        string posterId = null;
        foreach (RFP rfp in rfpList)
        {
            posterId = rfp.ExpertId;
        }
        string userId = WebUtil.GetIdentifier(Session["userId"]);
        if (posterId == userId)
        {
            btnRFPCreateVT.Visible = true;
            StringBuilder script = new StringBuilder();
            script.Append("<script>");
            script.Append("document.getElementById('ReplyRFP').style.display='none';");
            script.Append("</script>");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
        }
        if (Request["type"] == null)
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script>");
            script.Append("document.getElementById('R.F.P.sVirtualTeam').style.display='block';");
            script.Append("document.getElementById('RFPDetails').style.display='none';");
            script.Append("document.getElementById('top').style.display='none';");
            script.Append("document.getElementById('RFPsReply').style.display='none';");
            script.Append("document.getElementById('ReplyRFP').style.display='none';");
            script.Append("</script>");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
        }
    }

    //根据RFPId绑定详细信息
    private void RFPDataBind()
    {
        RFPBO rfpBO = new RFPBOImpl();
        rfpList = rfpBO.GetEntityList("RFPId", RFPId,"RFPDate");
        dvRFPInfo.DataSource = rfpList;
        dvRFPInfo.DataBind();
    }

    //根据RFPId绑定回复信息
    private void RFPReplyDataBind()
    {

        RFPReplyBO rfpReplyBO = new RFPBOReplyImpl();
        List<RFPReply> rfpReplyList = rfpReplyBO.GetEntityList("RFPId", RFPId, "RFPReplyDate");
        GVRFPReply.DataSource = rfpReplyList;
        GVRFPReply.DataBind();
    }

    //根据RFPId绑定团队信息
    private void RFPTeamBind()
    {
        TeamBO teamBO = new TeamBOImpl();
        List<Team> teamList = teamBO.GetEntityList("TeamRFPId", RFPId,"TeamDate");
        GVTeamInfo.DataSource = teamList;
        GVTeamInfo.DataBind();
    }

    protected void btnRFPCreateVT_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("../VirtualTeam/VTManager.aspx?RFPId=" + RFPId);
    }

    //发布新RFP
    protected void btnReplyRFPSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string replyDetail = txtReplyRFP.Text.Trim();
        RFPReply rfpReply = new RFPReply()
        {
            RFPReplyContent = replyDetail,
            RFPId = RFPId,
            RFPReplyExpert = WebUtil.GetIdentifier(Session["userId"]),
        };
        RFPReplyBO rfpReplyBO = new RFPBOReplyImpl();
        bool result = rfpReplyBO.Add(rfpReply);
        if (result)
        {
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Response.Write("<script>alert('Reply Fail')</script>");
        }
    }
    //点击专家显示专家详细信息
    protected void LBreplier_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string expertId = bt.CommandArgument.ToString();
        Response.Redirect("../Expert/ExpertDetails.aspx?expertId=" + expertId);
    }
    //点击团队名称，显示团队详细信息
    protected void LBTeamName_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string teamId = bt.CommandArgument.ToString();
        Response.Redirect("../VirtualTeam/VTManager.aspx?teamId=" + teamId);
    }
    protected void GVRFPReply_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GVRFPReply.PageIndex = e.NewPageIndex;
        this.GVRFPReply.DataBind();
    }
    protected void btnRFPInfoBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script language=javascript>history.go(-2);</script>");
    }
}